<template>
  <div>
    <header-title :header-title="title" />
    <account-form @on-success-valid="handleSubmit" />
    <el-button
      type="primary"
      style="margin-bottom: 10px"
      @click="handleClick"
    >
      新增账户
    </el-button>
    <account-dialog
      :row="row"
      :password="password"
      :dialog-visible="visible"
      @cancel-dialog="handleCancelDialog"
      @confirm-dialog="handleConfirmDialog"
    />
    <el-table
      v-loading="loading"
      :data="AccountTableData()"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="ctime"
        label="创建时间"
        min-width="180"
        align="center"
      />
      <el-table-column
        prop="uname"
        label="姓名"
        min-width="130"
        align="center"
      />
      <el-table-column
        prop="tel"
        label="登录账号"
        min-width="150"
        align="center"
      />
      <el-table-column
        prop="gname"
        label="角色"
        min-width="150"
        align="center"
      />
      <!-- <el-table-column
        prop="address"
        label="所在区域"
        min-width="150"
        align="center"
      /> -->
      <!-- <el-table-column
        prop="updatetime"
        label="更新时间"
        min-width="180"
        align="center"
      /> -->
      <el-table-column
        prop="state"
        label="状态"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div>
            {{
              scope.row.state === 0 ? '正常' : '冻结'
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="150"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            plain
            @click="handleClick(scope.row)"
          >
            编辑
          </el-button>
          <!-- <el-button
            type="danger"
            size="mini"
            plain
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- <pagination
      :total="AccountDataCount()"
      @click-page="handleClickPage"
      @change-pagesize="handleSizeChange"
    /> -->
  </div>
</template>
<script>
import HeaderTitle from '@/components/HeaderTitle'
// import Pagination from '@/components/Pagination'
import AccountDialog from '@/components/AccountDialog'
import AccountForm from '@/components/AccountForm'
import mixin from '@/mixin/mixin'
export default {
  components: {
    HeaderTitle,
    AccountDialog,
    AccountForm
  },
  mixins: [mixin],
  data() {
    return {
      title: '账户管理',
      loading: false,
      page: 1,
      pagesize: 10,
      visible: false,
      row: {},
      password: ''
    }
  },
  mounted() {
    this.getAccountData({})
    this.getRoleData({})
  },
  methods: {
    handleSubmit(playload) {
      this.loading = true
      this.getAccountData(playload).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    handleClick(row) {
      let newRow = {}
      if (row.id) {
        this.password = row.password
        newRow = { ...row }
        newRow.password = ''
      }
      this.row = newRow
      this.visible = true
    },
    handleDelete() {

    },
    handleClickPage(page) {
      this.page = page
    },
    handleSizeChange(pagesize) {
      this.pagesize = pagesize
    },
    handleCancelDialog() {
      this.visible = false
    },
    handleConfirmDialog(playload) {
      this.loading = true
      this.editAccountData(playload).then((res) => {
        if (res.data.state === '000') {
          this.$message.success('编辑成功！')
          this.getAccountData({})
          this.loading = false
        } else {
          this.$message.error(res.data.msg)
        }
      })
      this.visible = false
    }
  }
}
</script>

